using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Web.UI;

namespace Tyng.Web.UI.YUI
{
    public class MenuItem : IStateManager
    {
        bool _isTrackingViewState;
        StateBag _viewState;
        Submenu _submenu;

        [JsonIdentifier("submenu")]
        public Submenu Submenu
        {
            get
            {
                return _submenu;
            }
            set
            {
                _submenu = value;

                if (IsTrackingViewState && _submenu != null)
                    _submenu.TrackViewState();
            }
        }

        [JsonIdentifier("checked")]
        public bool Checked
        { 
            get 
            {
                object o = ViewState["Checked"];
                if (o == null)
                    return false;

                return (bool)o;
            }
            set
            {
                ViewState["Checked"] = value;
            }
        }

        [JsonIdentifier("disabled")]
        public bool Disabled
        {
            get
            {
                object o = ViewState["Disabled"];
                if (o == null)
                    return false;

                return (bool)o;
            }
            set
            {
                ViewState["Disabled"] = value;
            }
        }

        [JsonIdentifier("emphasis")]
        public bool Emphasis
        {
            get
            {
                object o = ViewState["Emphasis"];
                if (o == null)
                    return false;

                return (bool)o;
            }
            set
            {
                ViewState["Emphasis"] = value;
            }
        }

        [JsonIdentifier("selected")]
        public bool Selected
        {
            get
            {
                object o = ViewState["Selected"];
                if (o == null)
                    return false;

                return (bool)o;
            }
            set
            {
                ViewState["Selected"] = value;
            }
        }

        [JsonIdentifier("strongemphasis")]
        public bool StrongEmphasis
        {
            get
            {
                object o = ViewState["StrongEmphasis"];
                if (o == null)
                    return false;

                return (bool)o;
            }
            set
            {
                ViewState["StrongEmphasis"] = value;
            }
        }

        [JsonIdentifier("helptext")]
        public string HelpText
        {
            get
            {
                return (string)ViewState["HelpText"];
            }
            set
            {
                ViewState["HelpText"] = value;
            }
        }

        [JsonIdentifier("target")]
        public string Target
        {
            get
            {
                return (string)ViewState["Target"];
            }
            set
            {
                ViewState["Target"] = value;
            }
        }

        [JsonIdentifier("text")]
        public string Text
        {
            get
            {
                return (string)ViewState["Text"];
            }
            set
            {
                ViewState["Text"] = value;
            }
        }

        [JsonIdentifier("url")]
        public string Url
        {
            get
            {
                return (string)ViewState["Url"];
            }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    string temp = UrlPath.ResolveUrl(value);

                    ViewState["Url"] = temp;
                }
            }
        }

        //IMenuConfig Submenu { get; }
        #region Viewstate
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(false)]
        protected StateBag ViewState
        {
            get
            {
                if (this._viewState == null)
                {
                    this._viewState = new StateBag(false);
                    if (this._isTrackingViewState)
                    {
                        ((IStateManager)this._viewState).TrackViewState();
                    }
                }
                return this._viewState;
            }
        }

        protected virtual void LoadViewState(object savedState)
        {
            if (savedState != null)
            {
                Pair p = (Pair)savedState;

                ((IStateManager)ViewState).LoadViewState(p.First);

                if (p.Second != null)
                {
                    _submenu = new Submenu();
                    _submenu.LoadViewState(p.Second);
                }
            }
        }

        protected virtual object SaveViewState()
        {
            Pair p = new Pair();

            if (this._viewState != null)
            {
                p.First = ((IStateManager)_viewState).SaveViewState();
            }

            if (this._submenu != null)
            {
                p.Second = _submenu.SaveViewState();
            }

            return p;
        }

        protected virtual void TrackViewState()
        {
            this._isTrackingViewState = true;
            if (this._viewState != null)
            {
                ((IStateManager)_viewState).TrackViewState();
            }
            if (_submenu != null)
            {
                _submenu.TrackViewState();
            }
        }

        protected virtual bool IsTrackingViewState
        {
            get
            {
                return this._isTrackingViewState;
            }
        }

        protected internal void SetDirty()
        {
            if (this._viewState != null)
                this._viewState.SetDirty(true);

            if (_submenu != null)
                _submenu.SetDirty();
        }
        #endregion

        #region IStateManager Members

        bool IStateManager.IsTrackingViewState
        {
            get { return IsTrackingViewState; }
        }

        void IStateManager.LoadViewState(object state)
        {
            LoadViewState(state);
        }

        object IStateManager.SaveViewState()
        {
            return SaveViewState();
        }

        void IStateManager.TrackViewState()
        {
            TrackViewState();
        }

        #endregion
    }

    public class MenuItemCollection : StateManagedCollection
    {
        public int Add(MenuItem mi)
        {
            return ((IList)this).Add(mi);
        }

        public void AddRange(IEnumerable<MenuItem> menuItems)
        {
            foreach (MenuItem mi in menuItems)
                Add(mi);
        }

        protected override void SetDirtyObject(object o)
        {
            ((MenuItem)o).SetDirty();
        }

        public MenuItem this[int index]
        {
            get
            {
                return (MenuItem)((IList)this)[index];
            }
        }
    }
}